﻿using System;
using System.IO;
using SDT.CNFSystem;
using SDT.FileSystem;

namespace SDT.CrawlSystem.Servers
{
    /// <summary>
    /// Written by TungNX
    /// </summary>
    internal class Global
    {
        #region Properties from config file
        private ICnfReader cfgReader = null;

        public int Interval
        {
            get
            {
                lock (instance)
                {
                    try { return Convert.ToInt32(cfgReader["Interval"]); }
                    catch { return 10000; }
                }
            }
        }

        public int WorkUnitInterval
        {
            get
            {
                lock (instance)
                {
                    try { return Convert.ToInt32(cfgReader["WorkUnitInterval"]); }
                    catch { return 1500; }
                }
            }
        }

        public string LogFile
        {
            get
            {
                lock (instance)
                    return cfgReader["LogFile"];
            }
        }

        #region Folders
        public string DataFolder
        {
            get
            {
                lock (instance)
                    return cfgReader["DataFolder"];
            }
        }

        public string CacheFolder
        {
            get
            {
                lock (instance)
                    return cfgReader["CacheFolder"];
            }
        }

        public string HtmlFolder
        {
            get
            {
                lock (instance)
                    return cfgReader["HtmlFolder"];
            }
        }

        public string KlgFolder
        {
            get
            {
                lock (instance)
                    return cfgReader["KlgFolder"];
            }
        }
        #endregion

        public string Domain
        {
            get
            {
                lock (instance)
                    return cfgReader["Domain"];
            }
        }

        public string User
        {
            get
            {
                lock (instance)
                    return cfgReader["User"];
            }
        }

        public string Pass
        {
            get
            {
                lock (instance)
                    return cfgReader["Pass"];
            }
        }

        public string[] DataFiles
        {
            get
            {
                try
                {
                    return Directory.GetFiles(DataFolder, "*.web.channel");
                }
                catch
                {
                }

                return new string[] { };
            }
        }

        //Parameter Duplicate
        public double DuplicatedRating
        {
            get
            {
                lock (instance)
                {
                    try { return Convert.ToDouble(cfgReader["DuplicatedRating"]); }
                    catch { return 0.4; }
                }
            }
        }
        //Ngram hằng số tính độ trùng
        public int NGram
        {
            get
            {
                lock (instance)
                {
                    try { return Convert.ToInt16(cfgReader["NGram"]); }
                    catch { return 1; }
                }
            }
        }

        #endregion

        #region Enum Status

        public enum NewsStatus
        {
            NA = 0, NEW = 1
        };

        public enum DownloadStatus
        {
            NA = 0, NEW = 1
        };

        public enum FileType
        {
            NA = 0, IMG = 1
        };
        #endregion

        private Global() { cfgReader = new CnfReader(); }

        private static Global instance = new Global();
        internal static Global Instance
        {
            get { lock(instance) return Global.instance; }
        }

        public NetFileInfo GetFileInfo(string fname)
        {
            return FSController.TheController.GetFileInfo(fname, User, Domain, Pass);
        }

        public string ReadFile(string fname)
        {
            return FSController.TheController.ReadFile(fname, User, Domain, Pass);
        }

        public bool WriteFile(string fname, string content, bool append)
        {
            return FSController.TheController.WriteFile(fname, content, append, User, Domain, Pass);
        }
    }
}